//全局变量
var thispage=1;
var pages;

//运行顺序
arcalepz("typeshow",'name',"id",thispage,"suffix");

//ajks
//文档发送
function arcalepz(type,name,id,page,suffix) {
    $.ajax({
        url: "arcaletype",
        type: "post",
        dataType: "JSON",
        data: {
            type:type,
            name:name,
            id:id,
            page:page,
            suffix:suffix,
        },
        success: function(data) {

            if(data==false){
                console.log(11111);
                alert("类型已经存在")
            }else {
                showdate(data);
            }


        },
        error: function(err) {
            console.log(err)
        },
    });
}


//ajks响应调用
function  showdate(data){
    var tbody =document.getElementById("tbody").innerHTML='';
    pages=data.pages;
    for (var i=0;i<data.list.length;i++){
        tablecreateshow(data.list[i].arcaletypeid,data.list[i].arcaletypename,data.list[i].score);
    }
}

// 创建表格
function  tablecreateshow(id,type,score){
    var tbody =document.getElementById("tbody");

    var tr =document.createElement("tr");

    var td =document.createElement("td")
    td.innerHTML=id;
    td.id="id"+id;

    var showtype =document.createElement("td");
    showtype.innerHTML=type;
    showtype.id="showtype"+id;

    var upscore =document.createElement("td")
    upscore.innerHTML=score;
    upscore.id="upscore"+id;

    var option =document.createElement("td");

    var del =document.createElement("input");
    del.type='button';
    del.value="删除";
    del.onclick=function (){
    var id =document.getElementById(td.id).innerHTML;
        arcalepz("del","name",id,thispage,'arcalepz');
    }

    var update =document.createElement("input");
    update.type='button';
    update.value="修改";
    update.onclick=function (){
        document.getElementById("upid").innerHTML=document.getElementById(td.id).innerHTML;
        document.getElementById("upScore").value=document.getElementById( upscore.id).innerHTML;
        document.getElementById("upsuffix").value=document.getElementById(showtype.id).innerHTML;
        $('#update').modal('toggle', 'center');
    }

    tr.appendChild(td);
    tr.appendChild(showtype);
    option.appendChild(del);
    option.appendChild(update);
    tr.appendChild(upscore);
    tr.appendChild(option);
    tbody.appendChild(tr);
}

//按钮监听
function add(){
    $('#suffixadd').modal('toggle', 'center');
}
function suffixadd(){
    var suffixnsmr=document.getElementById("suffix").value;
    var Score =document.getElementById("score").value;
    arcalepz("add",suffixnsmr,Score,thispage,'arcalepz');
    $('#suffixadd').modal('hide', 'fit')
}
function suffixup(){
    var suffixnsmr=document.getElementById("upsuffix").value;
    var Score =document.getElementById("upScore").value;
    var id =document.getElementById("upid").innerHTML;
    arcalepz("update",suffixnsmr,id,thispage,Score);
    $('#update').modal('hide', 'fit')
}

function Arcalefron(){
    if(thispage-1>0){
        thispage=(thispage-1);
        arcalepz("typeshow",'name',"id",thispage,'arcalepz');
    }else {
        alert("没有上一页");
    }
}
function Arcalefronnext() {
    if (thispage < pages) {
        thispage = (thispage + 1);
        arcalepz("typeshow",'name',"id",thispage,'arcalepz');
    } else {
        alert("没有下一页");
    }
}